package com.itqf.controller;

import com.itqf.entity.Product;
import com.itqf.service.ProductService;
import com.itqf.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/10/12
 * @Time: 下午3:38
 */
@Controller
@RequestMapping("/product")
@Api(description = "商品操作")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/findAll")
    @ResponseBody
    @ApiOperation(value = "查询所有商品")
    public List<Product>  findAll(){
        return  productService.findAll();
    }


    @RequestMapping("/add")
    @ResponseBody
    @ApiOperation(value = "新增商品")
    public R add(@RequestBody @ApiParam("商品对象") Product product){
        product.setTid(1);
        product.setPtime(new Date());

       int i =  productService.add(product);
        if (i>0){
            return R.ok("新增成功");
        }
        return R.error("新增失败");
    }

    @RequestMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "修改商品")
    public  R  delete(@ApiParam(value = "商品编号",name = "id") int id){
        int i = productService.delete(id);
        if (i>0){
            return  R.ok("删除成功");
        }

        return  R.error("删除失败");
    }


}
